package com.williamoneil.charts.client;

import java.util.HashSet;
import java.util.Set;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootLayoutPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Charts implements EntryPoint {

	private static ChartMainPanel chartMainPanel = null; 
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		
		final DockLayoutPanel mainDock = new DockLayoutPanel(Unit.EM);
		//mainDock.setHeight("100%");
		//mainDock.setWidth("100%");

		mainDock.addNorth(new Label("Stock Chart"), 3);

		if(chartMainPanel == null) {
			chartMainPanel = new ChartMainPanel();
			//chartMainPanel.setSize("100%", "100%");
		}
		
		mainDock.add(chartMainPanel);
		
		// Use RootPanel.get() to get the entire body element
		RootLayoutPanel.get().add(mainDock);
	}

	/**
	 * @return the chartMainPanel
	 */
	public static ChartMainPanel getChartMainPanel() {
		return chartMainPanel;
	}
	
	public static void showError(Throwable ex) {
		String message = null;
		if(ex == null || ex.getMessage() == null) {
			message = "null?";
		} else {
			message = ex.getMessage();
		}
		Window.alert("Exception: " + message);
	}
	
	public static void showError(String msg) {
		Window.alert("Error Message: " + msg);
	}
	
	private static Set<Integer> submitKeyCodes = null;
	public static final Set<Integer> getSubmitKeyCodes() {
		if(submitKeyCodes == null) {
			submitKeyCodes = new HashSet<Integer>();
			submitKeyCodes.add(KeyCodes.KEY_ENTER);
			submitKeyCodes.add(10); //apparently this is the keycode for the 'return' key in iOS key-pad 
		}
		return submitKeyCodes;
	}
}
